package com.hubiao.basics.trytocase;

/**
 * Description
 *
 * @author hubiao
 * @since 2021-01-14 11:45
 */
public class TryToCaseDemo {
    /***
     * 1、不管try,finally都会执行；
     * 2、在try中return，在finally执行前会把结果保存起来，即使在finally中有修改也以try中保存的值为准
     * 3、如果try/finally都有return，直接返回finally中的return。
     *
     * @param args
     */
    public static void main(String[] args) {
        System.out.println( test1() );
        System.out.println( test2() );
        System.out.println( test3() );
        System.out.println( test4() );
        System.out.println( test5() );
    }

    private static int test1() {
        int i = 1;
        try {
            return i;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            i = 0;
        }
        return i;
    }

    private static int test2() {
        int i = 1;
        try {
            return i;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            i = 0;
            return i;
        }
    }

    private static User test3() {
        User user = new User( "u1" );
        try {
            return user;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            user = new User( "u2" );
        }
        return null;
    }

    private static User test4() {
        User user = new User( "u1" );
        try {
            return user;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            user.setName( "u2" );
        }
        return null;
    }

    private static String test5() {
        String str = "ab";
        try {
            return str;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            str = "abc";
        }
        return null;
    }

}

class User {

    private String name;

    public User(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }

}
